package online.xuping.center.account.impl;

import com.github.artbits.quickio.api.DB;
import com.github.artbits.quickio.core.QuickIO;
import online.xuping.center.account.*;
import online.xuping.center.account.domain.AccountDomain;
import online.xuping.center.account.domain.AccountGroupDomain;
import online.xuping.center.account.domain.AccountState;
import online.xuping.center.account.domain.AccountTypeDomain;
import online.xuping.center.account.domain.TradeType;
import online.xuping.common.IdUtils;
import online.xuping.common.SettingUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 账户服务
 */
@Service
public class AccountServiceImpl implements IAccountService {
    private DB db;


    private IAccountGroupService accountGroupService;

    private IAccountStateService accountStateService;

    public AccountServiceImpl( String dbName, IAccountGroupService accountGroupService, IAccountStateService accountStateService){
        this.db = QuickIO.usingDB(dbName);

        this.accountGroupService = accountGroupService;
        this.accountStateService = accountStateService;
    }
    public AccountServiceImpl( String dbName ){
        this.db = QuickIO.usingDB(dbName);

        this.accountGroupService = new AccountGroupServiceImpl();
        this.accountStateService = new AccountStateServiceImpl();
    }

    /**
     * 通过账户组创建用户账户
     * @param accountGroupDomain
     * @param uid
     * @return
     */
    @Override
    public List<AccountDomain> newAccountByGroup(AccountGroupDomain accountGroupDomain,Long uid) {
        if (accountGroupDomain == null) {
            throw AccountErrorCodeEnum.ACCOUNT_TYPE_NULL_ERROR.exception();
        }
        List<AccountDomain> re = new ArrayList<>();
        accountGroupDomain.getAccountTypeList().stream().forEach(it->{
            re.add(newAccount(it,uid));
        });
        return re;
    }

    /**
     * 通过账户组名字创建账户信息
     * @param accountGroupName
     * @param uid
     * @return
     */
    @Override
    public List<AccountDomain> newAccountByGroup(String accountGroupName, Long uid) {
        List<AccountGroupDomain> groups = accountGroupService.findAllGroup();
        AccountGroupDomain accountGroupDomain = groups.stream().filter(it -> accountGroupName.equals(it.getAccountGroupName())).findFirst().get();
        return newAccountByGroup(accountGroupDomain,uid);
    }

    /**
     * 通过账户类型,创建用户账户
     * @param accountTypeDomain
     * @param uid
     * @return
     */
    @Override
    public AccountDomain newAccount(AccountTypeDomain accountTypeDomain,Long uid) {

        AccountState accountState = accountStateService.initAccountState(accountTypeDomain.getAccountTypeName());
        AccountDomain accountDomain = new AccountDomain();
        accountDomain.setAccountType(accountTypeDomain);
        //accountTypeDomain 通过账户类型 获取可用交易类型
        accountDomain.setAccountCode(IdUtils.newId());
        accountDomain.setAccountState(accountState);
        accountDomain.setUid(uid);
        accountDomain.setBalance(0L);
        db.collection(AccountDomain.class).save(accountDomain);
        // TODO 需要将创建好的账户id写入到用户的个人信息对象中 （独立设计）
        return accountDomain;
    }
}
